//
//  Team9ExpertIdeabookColletionCell.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/16.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertIdeabookColletionCell.h"
#import "Team9ExpertIdeabookCell.h"
#import "Team9JQHeader.h"

@interface Team9ExpertIdeabookColletionCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, strong)UICollectionView *collectionView;
@end

@implementation Team9ExpertIdeabookColletionCell
static NSString *reuse = @"ExpertIdeabookCellReuse";
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(WIDTH / 2 - 15, WIDTH / 2 - 15);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self);
        make.bottom.equalTo(self);
    }];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.scrollEnabled = YES;
    self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
//#warning color!
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.collectionView registerClass:[Team9ExpertIdeabookCell class] forCellWithReuseIdentifier:reuse];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.cornerRadius = 5;
    [button setTitle:@"更多灵感画册" forState:UIControlStateNormal];
    UIColor *color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor grayColor];
    } else color = [UIColor orangeColor];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.layer.borderColor = color.CGColor;
    button.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    button.layer.borderWidth = 1;
    [self addSubview:button];
    [button addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-10);
        make.height.equalTo(@(WIDTH / 375 * 30));
        make.width.equalTo(button.mas_height).multipliedBy(4);
    }];
    
    
}
- (void)changecolor
{
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.collectionView.backgroundColor = [UIColor blackColor];
    } else self.collectionView.backgroundColor = [UIColor whiteColor];
}
- (void)more{
//    NSLog(@"test!");
    if (_Id && _vc) {
        self.vc.hidesBottomBarWhenPushed = YES;
        Team9ExpertMoreIdeabookVC *team9VC = [Team9ExpertMoreIdeabookVC new];
        team9VC.url = [NSString stringWithFormat:@"http://api.meilijia.com/app/user_home_ideabook_list?user_id=%@&page=1", _Id];
//        NSLog(@"test3: %@", team9VC.url);
        [self.vc.navigationController pushViewController:team9VC animated:YES];
    }
//    if (_moreBlock) {
//        self.moreBlock();
//    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Team9ExpertIdeabookCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    
    cell.model = self.array[indexPath.row];
    return cell;
}
- (void)setArray:(NSArray *)array
{
    if (_array != array) {
        _array = array;
        [self.collectionView reloadData];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"test3");
    if (_block) {
        _block(indexPath.row);
    }
}

@end
